package com.darwin.darwinMVC.mvc
{
	import com.darwin.darwinMVC.commands.SourceCommand;
	import com.darwin.darwinMVC.core.CmdNameList;
	import com.darwin.darwinMVC.core.Context;
	import com.darwin.darwinMVC.core.ContextPlace;
	import com.darwin.darwinMVC.core.IListener;
	
	import flash.utils.Dictionary;

	/**
	 * 一个功能模块中使用的数据类
	 * @author lipi
	 */
	public class ModelData implements IListener
	{
		public function ModelData()
		{
			_context = ContextPlace.getContext();
			_registerDic = new Dictionary();
			_dataDic = new Dictionary();
		}
		protected var _context:Context;
		
		//已被注册的命令列表
		private var _registerDic:Dictionary;
		
		//当前对象保存的数据列表
		private var _dataDic:Dictionary;
		
		
		
		
		/**
		 * @param cmd
		 */
		public function execute(cmd:Command):void
		{
			if(cmd is SourceCommand)
			{
				var $cmd:SourceCommand = cmd as SourceCommand;
				var $ds:DataSource = $cmd.dataSource;
				if(_dataDic[$cmd.cmdName] != $cmd.data)
				{//如果返回的结果和当前保存的一样则忽略
					for(var $name:Object in _registerDic)
					{//遍历存在的侦听
						var $b:Boolean = true;
						var $cmdNameList:CmdNameList = $name as CmdNameList;
						if($cmdNameList.exist($cmd.cmdName))
						{//如果侦听和这当前执行的命令有点则继续
							var $list:Array = $cmdNameList.nameList;
							var returnData:Object = {};
						
							for(var i:int = 0;i<$list.length;i++)
							{//
								var $data:Object = $ds.getData($list[i]);//取得数据
								if($data == null) $b = false;
								else _dataDic[$list[i]] = $data;
								
								returnData[$list[i]] = $data;
							}
							if($b == true)
							{
								(_registerDic[$name] as Function).call(this,returnData);
							}//if
						}//if
					}//for
				}//if
			}//if
		}
		
		
		/**
		 * 注册一个组合命令侦听，注意：同样的组合命令只会最后一次有效
		 * @param cmdNameList 命令名字列表
		 * @param fn 侦听函数，函数有一个参数，即返回的数据
		 */
		public function register(cmdNameList:CmdNameList,fn:Function,priority:int = 0):void
		{
			_registerDic[cmdNameList] = fn;
			for(var i:int = 0;i<cmdNameList.nameList.length;i++)
			{
				_context.addCmdListener(cmdNameList.nameList[i],this,priority);
			}
		}
		
		/**
		 * 从dataSource取得一个数据
		 */
		public function getDataBySource(dataName:String):Object
		{
			return _context.dataSource.getData(dataName);
		}
		
		
		/**
		 * 移除一个组合命令侦听
		 * @param cmdNameList 命令名字列表
		 */
		public function removeRegister(cmdNameList:CmdNameList):void
		{
			//函数功能是移除回调函数并从_context移除侦听器
			var $arr:Array = cmdNameList.nameList;
			var $cmdNameDic:Dictionary = new Dictionary();
			for(var i:int = 0;i<$arr.length;i++)
			{
				var $bool:Boolean = true;
				for(var item:Object in _registerDic)
				{
					if(cmdNameList.contrast(item as CmdNameList))
					{
						delete _registerDic[item];
					}else{
						if((item as CmdNameList).exist($arr[i]))
						{
							$bool = false;
						}
					}//if
				}//for
				if($bool)
				{
					_context.removeCmdListener($arr[i],this);
				}
			}
		}
		
		
		/**
		 * 锁定一个组合命令
		 * @param cmdNameList 组合命令名字列表 
		 */
		public function lockCmdList(cmdNameList:CmdNameList):void
		{
			
		}
		/**
		 * 解锁一个组合命令
		 * @param cmdNameList 组合命令名字列表
		 */
		public function unlockCmdList(cmdNameList:CmdNameList):void
		{
			
		}
		
		/**
		 * 锁定一个命令，使用lockCmd锁定一个命令会将所有组合命令中有这个命令的都不执行
		 * @param cmdName 命令名字
		 */
		public function lockCmd(cmdName:String):void
		{
			
		}
		
		/**
		 * 解锁一个命令
		 * @param cmdName 命令名字
		 */
		public function unlockCmd(cmdName:String):void
		{
			
		}
		
		
		/**
		 * 发布一个数据命令
		 * @param cmd 命令对象
		 */
		public function dispatch(cmd:Command):void
		{
			_context.dispatch(cmd);
		}
		
	}
}